Omplus.base.define(Omplus.controllers.Controller, {
    omplusName:"privilegeController"
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"PrivilegeSearch",
    controller:"privilegeController",
    value:{
        "name":{
            omplusType:"String"
        }
    }
});

Omplus.models.PrivilegeSearchModelProto = Omplus.base.extend(Omplus.models.Object, {
    omplusType:"PrivilegeSearchModelProto",
    value:{
        name:{
            omplusType:"String"
        }
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"PrivilegeSearchResults",
    controller:"privilegeController",
    value:{
        privileges:{
            omplusType:"Array",
            omplusProto:"PrivilegeSearchModelProto"
        }
    }
});

Omplus.base.define(Omplus.views.ManagementPanel, {
    omplusName:"privilegePanel",
    controller:"privilegeController",
    renderTo:"managements",
    models:{
        search:"PrivilegeSearch",
        searchResult:"PrivilegeSearchResults",
        searchResultList:"privileges"
    },
    views:{
        search:[
            {
                model:"PrivilegeSearch.name",
                labelModel:"Assets.list.privilege_label.localValue",
                inputType:"TextInput"
            },
            {
                // Per page
                inputType:"Select",
                model:"PerPages",
                showModel:"number",
                current:"forTransactionPlayers",
                controller:"languageController",
                labelModel:"Assets.list.label_per_page.localValue",
                perPage:true
            },
            {
                // Add button
                omplusType:"Button",
                omplusName:"AddPrivilegeButton",
                controller:"languageController",
                model:"Assets.list.label_add.localValue",
                click:function () {
                    Omplus.globals.getObject("AddNewPrivilege").show();
                }
            },
            {
                // Search button
                inputType:"Button",
                controller:"languageController",
                model:"Assets.list.label_search_button.localValue",
                cssClasses:["search-button"],
                labelWidth:"0%",
                inputWidth:"100%",
                searchButton:true
            }
        ],
        searchResult:[
            {
                labelModel:"Assets.list.privilege_label.localValue",
                width:"300px",
                omplusType:"Label",
                model:"name"
            },
            {
                omplusType:"Button",
                model:"Assets.list.label_delete.localValue",
                width:"160px",
                controller:"languageController",
                click:function () {
                    var asset = $(this).closest(".tableRow").find("label:first").text();
                    Omplus.globals.getObject("PrivilegeEditModel.name").setValue(asset);
                    Omplus.globals.getObject("DeletePrivilege").show();
                }
            }
        ]
    }
});

Omplus.base.define(Omplus.models.Model, {
    omplusName:"PrivilegeEditModel",
    controller:"privilegeController",
    value:{
        name:{
            omplusType:"String"
        }
    },
    actions:[
        {
            omplusType:"Get",
            omplusName:"addPrivilege",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"PrivilegeEditModel",
            responseModel:"PrivilegeSearchResults"
        },
        {
            omplusType:"Get",
            omplusName:"deletePrivilege",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"PrivilegeEditModel",
            callback:function () {
                $("#privilegePanel_searchButton_input").click();
            }
        },
        {
            omplusType:"Get",
            omplusName:"editPrivilege",
            proxy:{
                omplusType:"AjaxProxy"
            },
            requestModel:"PrivilegeEditModel",
            callback:function () {
                $("#privilegePanel_searchButton_input").click();
            }
        }
    ]
});

Omplus.base.define(Omplus.views.Dialog, {
    omplusName:"AddNewPrivilege",
    width:"500px",
    height:"300px",
    components:[
        {
            omplusType:"LabelInputPair",
            omplusName:"addPrivilegeName",
            labelModel:"Assets.list.privilege_label.localValue",
            model:"PrivilegeEditModel.name",
            controller:"privilegeController",
            labelController:"languageController"
        }
    ],
    confirmClick:function () {
        Omplus.globals.getObject("PrivilegeEditModel").getAction("addPrivilege").run();
        Omplus.globals.getObject("AddNewPrivilege").hide();
        $("#privilegePanel_searchButton_input").click();
    }
});

Omplus.base.define(Omplus.views.Dialog, {
    omplusName:"DeletePrivilege",
    width:"500px",
    height:"300px",
    components:[
        {
            omplusType:"Label",
            omplusName:"deletePrivilegeQuestion",
            model:"Assets.list.delete_asset_question_label.localValue",
            controller:"languageController"
        }
    ],
    confirmClick:function () {
        Omplus.globals.getObject("PrivilegeEditModel").getAction("deletePrivilege").run();
        Omplus.globals.getObject("DeletePrivilege").hide();
        $("#privilegePanel_searchButton_input").click();

    }
});